package com.example.k12.app

import android.app.Application
import android.content.Context
import android.graphics.Color
import android.view.Gravity
import com.blankj.utilcode.util.CrashUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.blankj.utilcode.util.Utils
import com.example.k12.R
import com.example.k12.utlis.WxPayUtils
import com.example.k12.utlis.db.DBManager
import me.jessyan.autosize.AutoSize
import androidx.core.graphics.toColorInt

class MyApp : Application() {
    companion object {
        @JvmStatic
        private lateinit var instance: MyApp

        /**
         * 获取全局Application Context
         */
        @JvmStatic
        fun getContext(): Context {
            return instance
        }
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        initAutoSize()
        WxPayUtils.initPayFace(this)
        Utils.init(this)
        initToast()
        initLog()
        initCrash()
        initDB()
    }

    private fun initDB() {
        // 初始化数据库
        DBManager.getInstance().init()
    }

    private fun initAutoSize() {
        AutoSize.initCompatMultiProcess(this)
        //初始化适配方案
        AutoSize.checkAndInit(this)
    }

    private fun initToast() {
        ToastUtils.getDefaultMaker()
            .setBgColor("#cc000000".toColorInt())
            .setNotUseSystemToast()
            .setTextColor(Color.WHITE)
            .setGravity(
                Gravity.BOTTOM or Gravity.CENTER,
                0,
                resources.getDimensionPixelOffset(R.dimen.dp50)
            )
    }

    private fun initLog() {
        LogUtils.getConfig()
            .setLogHeadSwitch(false)
            .setBorderSwitch(false)
            .setLog2FileSwitch(true)
            .setConsoleSwitch(true)
            .setFileFilter(LogUtils.D)
            .setLogSwitch(true)
            .setFilePrefix("wxk12")
    }

    private fun initCrash() {
        CrashUtils.init()
    }
}
